use tokio::sync::broadcast;
use std::sync::Arc;

#[derive(Clone)]
pub struct MessageQueue<T: Clone + Send + 'static> {
    sender: Arc<broadcast::Sender<T>>,
}

impl<T: Clone + Send + 'static> MessageQueue<T> {
    pub fn new(capacity: usize) -> Self {
        let (sender, _) = broadcast::channel(capacity);
        Self {
            sender: Arc::new(sender),
        }
    }

    pub fn send(&self, value: T) -> Result<usize, broadcast::error::SendError<T>> {
        self.sender.send(value)
    }

    pub fn subscribe(&self) -> broadcast::Receiver<T> {
        self.sender.subscribe()
    }

    pub fn receiver_count(&self) -> usize {
        self.sender.receiver_count()
    }
}